<template>
  <div class="resource-model">
    <m-resource
      @getData="getData"
      :resourceData="resourceData"
      :noShowBtn="true"
    ></m-resource>
  </div>
</template>

<script>
  import _ from 'lodash'
  import mResource from '@/module/components/transfer/resource'
  import { mapActions } from 'vuex'
  export default {
    name: 'resource',
    components: { mResource },
    data () {
      return {
        resourceData: {
          fileSourceList: [],
          udfSourceList: [],
          fileTargetList: [],
          udfTargetList: [],
          type: {
            name: ''
          }
        },
        resource: {}
      }
    },
    methods: {
      ...mapActions('security', ['deleteUser', 'getAuthList', 'grantAuthorization', 'getResourceList']),
      _authFile (item, i) {
        this.getResourceList({
          id: 90,
          type: 'file',
          category: 'resources'
        }).then(data => {
          let fileSourceList = []
          let udfSourceList = []
          data[0].forEach((value, index, array) => {
            if (value.type === 'FILE') {
              fileSourceList.push(value)
            } else {
              udfSourceList.push(value)
            }
          })
          let fileTargetList = []
          let udfTargetList = []
          let pathId = []
          data[1].forEach(v => {
            let arr = []
            arr[0] = v
            if (this.getAllLeaf(arr).length > 0) {
              pathId.push(this.getAllLeaf(arr)[0])
            }
          })
          data[1].forEach((value, index, array) => {
            if (value.type === 'FILE') {
              fileTargetList.push(value)
            } else {
              udfTargetList.push(value)
            }
          })
          fileTargetList = _.map(fileTargetList, v => {
            return v.id
          })
          udfTargetList = _.map(udfTargetList, v => {
            return v.id
          })
          this.item = item
          this.resourceData.fileSourceList = fileSourceList
          this.resourceData.udfSourceList = udfSourceList
          this.resourceData.fileTargetList = fileTargetList
          this.resourceData.udfTargetList = udfTargetList
        }).catch(e => {
          this.$message.error(e.msg || '')
        })
      },
      /*
        getAllLeaf
       */
      getAllLeaf (data) {
        let result = []
        let getLeaf = (data) => {
          data.forEach(item => {
            if (item.children.length === 0) {
              result.push(item)
            } else {
              getLeaf(item.children)
            }
          })
        }
        getLeaf(data)
        return result
      },
      getData (res) {
        this.resource = res
      }
    },
    watch: {
      resource: function (v) {
        this.$store.commit('permission/setPermissions', {
          resource: this.resource
        })
      }
    },
    created () {
      this._authFile()
    },

  }
</script>
<style scoped lang='scss'>
/deep/.resource-model {
  .el-tabs__nav {
    border: none !important;
    border-left: none !important;
    border-top-left-radius: none !important;
    border-bottom-left-radius: none !important;
    border-bottom: 2px solid #e5e5e5 !important;
    .el-tabs__active-bar {
      width: 57px !important;
    }
  }
  .el-tabs__nav.is-top {
    border-bottom: 2px solid #e5e5e5 !important;
  }
  .el-tabs__item {
    height: 30px !important;
    line-height: 30px !important;
    border: none !important;
    border-bottom: 1px solid #e5e5e5 !important;
    color: #33317e !important;
    &:last-child {
      border-top-right-radius: none !important;
      border-bottom-right-radius: none !important;
    }
    &:first-child {
      border-top-left-radius: none !important;
      border-bottom-left-radius: none !important;
    }
    &.is-active {
      background-color: #fff !important;
      color: #33317e !important;
    }
  }
}
</style>
